package DaEr3_2;

import java.util.Random;
import java.util.Scanner;

public class ZhangHuAndMiMa {
    public static void main(String[] args) {
        String username="lilidawang";
        String pw="0729666";
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入用户名：");
            String inputname = scanner.next();
            System.out.println("请输入密码：");
            String inputpass = scanner.next();
            String computerCode=ZhangHuAndMiMa.yanzheng();
            System.out.println("电脑产生的验证码是："+computerCode);
            System.out.println("请输入验证码：");
            String inputcode1 = scanner.next();
            while (!inputcode1.equals(computerCode)) {
                Scanner scanner1 = new Scanner(System.in);
                System.out.println("验证码错误，请重新输入：");
                 computerCode=ZhangHuAndMiMa.yanzheng();
                System.out.println("电脑产生的验证码是："+computerCode);
                inputcode1=scanner1.next();
            }
            if (inputname.equals(username)&&inputpass.equals(pw)){
                System.out.println("登陆成功");
                break;
            }else {
                if (i==2){
                    System.out.println("账号"+inputname+"被锁定，请联系管理员");
                }else {
                    System.out.println("账号或密码错误，你还剩下"+(2-i)+"次机会");
                }
            }
        }
    }
    public static String yanzheng() {
        char[]code={'A','B','C','a','x','y','1','2','3','#','@'};
        Random random = new Random();
        String res="";
        for (int i = 0; i < 4; i++) {
            int index = random.nextInt(code.length);
            res+=code[index];
        }
        return  res;
    }
}
